package cn.wolfcode.crud.web.controller;

import cn.wolfcode.crud.qo.CustomerReportQuerObjcet;
import cn.wolfcode.crud.service.ICustomerReportService;
import cn.wolfcode.crud.util.MessageUtil;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//专门处理报表的东西
@Controller
@RequestMapping("/customerReport")
public class CustomerReportController {
    @Autowired
    private ICustomerReportService customerReportService;

    //处理高级查询和跳转页面
    @RequiresPermissions(value = {"customerReport:list","报表明细"},logical = Logical.OR)
    @RequestMapping("/list")
    public String list(Model model,@ModelAttribute("qo") CustomerReportQuerObjcet qo){
        //处理高级查询
        PageInfo<HashMap> pageInfo = customerReportService.query(qo);
        model.addAttribute("PageResult",pageInfo);
        return "/customerReport/list";
    }
    //柱状图处理高级查询不分页和跳转页面
    @RequestMapping("/listByBar")
    public String listByBar(Model model,@ModelAttribute("qo") CustomerReportQuerObjcet qo){
        //处理高级查询
        //1执行sql把所有数据查询出来
        List<HashMap> data=customerReportService.selectAll(qo);
        //准备2个x和y的list集合
        ArrayList xlist = new ArrayList();
        ArrayList ylist = new ArrayList();
        //把数据查询出来放在list集合中
        for (HashMap map : data) {
            xlist.add(map.get("groupType"));//key值放在x中,value值放在y中
            ylist.add(map.get("counts"));
        }
        //共享两个集合数据
        model.addAttribute("xlist", JSON.toJSONString(xlist));//把集合转换成json字符串
        model.addAttribute("ylist",JSON.toJSONString(ylist));
        //分组类型转为文字显示
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listByBar";
    }
    //状图处理高级查询不分页和跳转页面listByPie
    @RequestMapping("/listByPie")
    public String listByPie(Model model,@ModelAttribute("qo") CustomerReportQuerObjcet qo){
        //1执行sql把所有数据查询出来
        List<HashMap> data=customerReportService.selectAll(qo);
        //准备2个x和y的list集合,一个是左上角,一个是图像
        ArrayList groupTypeValues = new ArrayList();
        ArrayList countsValues = new ArrayList();
        //拿到每个数字放进去
        for (HashMap map : data) {
            //第一个集合
            groupTypeValues.add(map.get("groupType"));
            //创建一个map集合来放key和value值
            HashMap<Object, Object> hashMap = new HashMap<>();
            hashMap.put("value",map.get("counts"));
            hashMap.put("name",map.get("groupType"));
            countsValues.add(hashMap);
        }
        model.addAttribute("groupTypeValues",JSON.toJSONString(groupTypeValues));
        model.addAttribute("countsValues",JSON.toJSONString(countsValues));
        //分组类型转为文字显示
        model.addAttribute("groupTypeName", MessageUtil.changMsg(qo));
        return "/customerReport/listByPie";
    }
}
